//
//  JONewfeatureViewController.m
//  新浪微博模拟
//
//  Created by song on 15/4/14.
//  Copyright (c) 2015年 song. All rights reserved.
//

#import "JONewfeatureViewController.h"
#import "JOTabBarViewController.h"

#define  JOPageCount 3

@interface JONewfeatureViewController () <UIScrollViewAccessibilityDelegate>

@property(nonatomic, weak) UIPageControl *pageControl;

@end

@implementation JONewfeatureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self setupScrollView];
    
    [self setupPageControll];
   
}


-(void) setupPageControll
{
    UIPageControl *pageControl =[[UIPageControl alloc] init];
    pageControl.numberOfPages =JOPageCount;
    CGFloat centerX =self.view.frame.size.width * 0.5;
    CGFloat centerY = self.view.frame.size.height - 20;
    
    pageControl.center =CGPointMake(centerX, centerY);
    pageControl.bounds =CGRectMake(0, 0, 100, 30);
    
    self.pageControl = pageControl;
    
    [self.view addSubview:pageControl];
    pageControl.currentPageIndicatorTintColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"new_feature_pagecontrol_checked_point"]];
    pageControl.pageIndicatorTintColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"new_feature_pagecontrol_point"]];
    
    
    
}


-(void) setupScrollView
{
    UIImageView *bg =[[UIImageView alloc] init];
    bg.image =[UIImage imageNamed:@"new_feature_background"];
    bg.frame = self.view.bounds;
    [self.view addSubview:bg];
    
    [self.view setBackgroundColor:[UIColor redColor]];
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = self.view.bounds;
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
    
    CGFloat scrollW = scrollView.frame.size.width;
    CGFloat scrollH = scrollView.frame.size.height;
    for(int index =0 ;index <3 ;index++)
    {
        UIImageView *imageview = [[UIImageView alloc] init];
        NSString *name = [NSString stringWithFormat:@"new_feature_%d" , index+1];
        imageview.image = [UIImage  imageNamed:name];
        
        CGFloat imageX = index *scrollW;
        
        imageview.frame = CGRectMake(imageX, 0, scrollW, scrollH);
        [scrollView addSubview:imageview];
        
        if(index == JOPageCount -1)
        {
            [self setupLastImageView:imageview];
        }
        
    }
    
    scrollView.contentSize = CGSizeMake(scrollW * 3, 0);
    scrollView.showsHorizontalScrollIndicator =NO;
    scrollView.pagingEnabled = YES;
    scrollView.bounces = NO;
}



-(void)setupLastImageView:(UIImageView *)imageview
{
    
    imageview.userInteractionEnabled =YES;
    UIButton *startBtn = [[UIButton alloc] init];
    
    [startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button"] forState:UIControlStateNormal];
    
    [startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
    
    CGFloat centerX = imageview.frame.size.width * 0.5;
    CGFloat centerY = imageview.frame.size.height * 0.5;
    
    startBtn.center = CGPointMake(centerX, centerY);
    
    startBtn.bounds =(CGRect){CGPointZero , startBtn.currentBackgroundImage.size};
    [startBtn setTitle:@"开始微博" forState:UIControlStateNormal];
    [imageview addSubview:startBtn];
    [startBtn addTarget:self action:@selector(showPage) forControlEvents:UIControlEventTouchUpInside];
}

-(void)showPage
{
    self.view.window.rootViewController =[[JOTabBarViewController alloc] init];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetX = scrollView.contentOffset.x;
    
    double pageDouble =offsetX /scrollView.frame.size.width;
    
    int pageInt = (int)(pageDouble + 0.5);
    
    self.pageControl.currentPage = pageInt;
    //NSLog(@"  %f , %d" ,pageDouble , pageInt);
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

    
}



@end
